package com.newshub.admin.controller;

import com.newshub.common.Result;
import com.newshub.business.dto.NewsQueryDTO;
import com.newshub.business.dto.PageResult;
import com.newshub.business.entity.News;
import com.newshub.business.service.NewsService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员新闻管理控制器
 */
@RestController
@RequestMapping("/api/admin/news")
@CrossOrigin(origins = "*") // 允许跨域访问
public class AdminNewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 分页查询新闻列表
     */
    @PostMapping("/page")
    @BusinessLog(module = "新闻管理", description = "分页查询新闻列表", level = LogLevel.INFO)
    public Result<PageResult<News>> getNewsPage(@RequestBody NewsQueryDTO queryDTO) {
        try {
            PageResult<News> result = newsService.getNewsPage(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询新闻列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询新闻详情
     */
    @GetMapping("/{id}")
    @BusinessLog(module = "新闻管理", description = "查询新闻详情", level = LogLevel.INFO)
    public Result<News> getNewsById(@PathVariable Integer id) {
        try {
            News news = newsService.getNewsById(id);
            if (news == null) {
                return Result.error("新闻不存在");
            }
            return Result.success(news);
        } catch (Exception e) {
            return Result.error("查询新闻详情失败: " + e.getMessage());
        }
    }

    /**
     * 新增新闻
     */
    @PostMapping
    @BusinessLog(module = "新闻管理", description = "新增新闻", level = LogLevel.INFO)
    public Result<String> addNews(@RequestBody News news) {
        try {
            boolean success = newsService.addNews(news);
            if (success) {
                return Result.success("新增新闻成功");
            } else {
                return Result.error("新增新闻失败");
            }
        } catch (Exception e) {
            return Result.error("新增新闻失败: " + e.getMessage());
        }
    }

    /**
     * 更新新闻
     */
    @PutMapping("/{id}")
    @BusinessLog(module = "新闻管理", description = "更新新闻", level = LogLevel.INFO)
    public Result<String> updateNews(@PathVariable Integer id, @RequestBody News news) {
        try {
            news.setId(id);
            boolean success = newsService.updateNews(news);
            if (success) {
                return Result.success("更新新闻成功");
            } else {
                return Result.error("更新新闻失败");
            }
        } catch (Exception e) {
            return Result.error("更新新闻失败: " + e.getMessage());
        }
    }

    /**
     * 删除新闻
     */
    @DeleteMapping("/{id}")
    @BusinessLog(module = "新闻管理", description = "删除新闻", level = LogLevel.WARN)
    public Result<String> deleteNews(@PathVariable Integer id) {
        try {
            boolean success = newsService.deleteNews(id);
            if (success) {
                return Result.success("删除新闻成功");
            } else {
                return Result.error("删除新闻失败");
            }
        } catch (Exception e) {
            return Result.error("删除新闻失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除新闻
     */
    @DeleteMapping("/batch")
    @BusinessLog(module = "新闻管理", description = "批量删除新闻", level = LogLevel.WARN)
    public Result<String> batchDeleteNews(@RequestBody List<Integer> ids) {
        try {
            boolean success = newsService.batchDeleteNews(ids);
            if (success) {
                return Result.success("批量删除新闻成功");
            } else {
                return Result.error("批量删除新闻失败");
            }
        } catch (Exception e) {
            return Result.error("批量删除新闻失败: " + e.getMessage());
        }
    }

    /**
     * 更新新闻状态
     */
    @PutMapping("/{id}/status")
    @BusinessLog(module = "新闻管理", description = "更新新闻状态", level = LogLevel.INFO)
    public Result<String> updateNewsStatus(@PathVariable Integer id, @RequestParam Integer status) {
        try {
            boolean success = newsService.updateNewsStatus(id, status);
            if (success) {
                return Result.success("更新新闻状态成功");
            } else {
                return Result.error("更新新闻状态失败");
            }
        } catch (Exception e) {
            return Result.error("更新新闻状态失败: " + e.getMessage());
        }
    }

    /**
     * 更新新闻热门状态
     */
    @PutMapping("/{id}/hot")
    @BusinessLog(module = "新闻管理", description = "更新新闻热门状态", level = LogLevel.INFO)
    public Result<String> updateNewsHot(@PathVariable Integer id, @RequestParam Integer isHot) {
        try {
            boolean success = newsService.updateNewsHot(id, isHot);
            if (success) {
                return Result.success("更新新闻热门状态成功");
            } else {
                return Result.error("更新新闻热门状态失败");
            }
        } catch (Exception e) {
            return Result.error("更新新闻热门状态失败: " + e.getMessage());
        }
    }
}
